<!DOCTYPE html>
<html>
<head lang="en">
	<meta charset="UTF-8">
<title>带关闭按钮的滚动广告</title>
<style type="text/css">
#main{ text-align:center;}
#float{
	position:absolute;
	left:30px;
	top:60px; 
	z-index:1;
	}
#close{
	position:absolute;
	top:60px;
	left:134px;
	z-index:2;
	cursor:hand;
}
</style>
</head>
<body>
<div id="close" onClick="adv_close()"><img src="images/close.jpg"></div>
<div id="float"><img src="images/advpic.jpg"></div>
<div id="main"><img src="images/contentpic.jpg"></div>
<script>
	var adverTop; //层距页面顶端距离
	var adverLeft;
	var adverObj; //层对象

	var closeTop; //关闭按钮顶端距离
	var closeLeft;
	var closeObj; //关闭按钮对象

	// 关闭广告
	function adv_close(){
		adverObj=document.getElementById("float"); //获得层对象
		adverObj.style.display = "none";
		closeObj=document.getElementById("close"); //获得关闭按钮对象
		closeObj.style.display = "none";
	}

	function inix(){
		adverObj=document.getElementById("float"); //获得层对象
		closeObj=document.getElementById("close"); //获得关闭按钮对象
		if(adverObj.currentStyle){  // IE浏览器
			adverTop=parseInt(adverObj.currentStyle.top);
			adverLeft=parseInt(adverObj.currentStyle.left);

			closeTop=parseInt(closeObj.currentStyle.top);
			closeLeft=parseInt(closeObj.currentStyle.left);
			
		}
		// 非ie浏览器
		else{
			adverTop=parseInt(document.defaultView.getComputedStyle(adverObj,null).top);
			adverLeft=parseInt(document.defaultView.getComputedStyle(adverObj,null).left);

			closeTop=parseInt(document.defaultView.getComputedStyle(closeObj,null).top);
			closeLeft=parseInt(document.defaultView.getComputedStyle(closeObj,null).left);
		}
	}

	function move(){
        var sTop=parseInt(document.documentElement.scrollTop||document.body.scrollTop);
        var sLeft=parseInt(document.documentElement.scrollLeft||document.body.scrollLeft);


		adverObj.style.top=adverTop+sTop+"px";
		adverObj.style.left=adverLeft+sLeft+"px";

		closeObj.style.top=closeTop+sTop+"px";
		closeObj.style.left=closeLeft+sLeft+"px";
	}
	window.onload=inix;
	window.onscroll=move; // 可以监听滚动条移动
</script>
</body>
</html>
